﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Scheduling.Models;
using Scheduling.Models.Interfaces;
using Scheduling.Models.Helpers;
using Scheduling.Models.Repos.InMemory;

namespace MultipleBooking
{
    class Program
    {
        public static List<BookingRequest> Requests;
        public static List<User> UserList;

        public static MultipleBooker MultiBooker;
        
        static void Main(string[] args)
        {
            Requests = new List<BookingRequest>();

            IoC.CreateEmptyContainer();
            IoC.RegisterType<IUserLoader, InMemoryUserLoader>();
            IoC.RegisterType<ICourseLoader, InMemoryCourseLoader>();
            IoC.RegisterType<IRoomLoader, InMemoryRoomLoader>();
            IoC.RegisterType<IBookingLoader, InMemoryBookingLoader>();
            IoC.RegisterType<IRoomPropertyLoader, InMemoryRoomPropertyLoader>();
            IoC.RegisterType<IDepartmentLoader, InMemoryDepartmentLoader>();
            IoC.RegisterType<IUserGroupLoader, InMemoryUserGroupLoader>();
            IoC.RegisterType<ICourseInstanceLoader, InMemoryCourseInstanceLoader>();
            IoC.RegisterType<IBookingRequestLoader, InMemoryBookingRequestLoader>();

            MultiBooker = new MultipleBooker();

            System.Windows.Forms.Application.Run(new GeneratorWindow());            
        }

        public static void GenerateRandomStuff(int BookerCount, int RoomCount, int RequestCount, int Seed)
        {
            Random Rand = new Random(Seed);

            Requests.Clear();
            MultiBooker.RoomList.Clear();

            var bookerList = new List<User>();
            for (int i = 0; i < BookerCount; i++)
            {
                User u = new User()
                {
                    ID = (((char)Rand.Next(65, 90)).ToString() + ((char)Rand.Next(65, 90))).ToString()
                };
                Console.WriteLine("Generated User: " + u.ID);
                bookerList.Add(u);
            }

            for (int i = 0; i < RoomCount; i++)
            {
                MultiBooker.RoomList.Add(
                    new Room()
                    {
                        ID = "21" + ((char)Rand.Next(65, 71)).ToString() + Rand.Next(0, 6) + Rand.Next(0, 20).ToString("00"),
                        Type = RoomType.LectureHall,
                        Capacity = RandRange(Rand, 30, 30, 30, 30, 30, 50, 50, 90, 200, 200)
                    });
                Console.WriteLine(string.Format("Generated Room: {0} Capacity: {1}", MultiBooker.RoomList.Last().ID, MultiBooker.RoomList.Last().Capacity));
            }

            for (int i = 0; i < RequestCount; i++)
            {
                Requests.Add(new BookingRequest()
                {
                    Booker = bookerList.ElementAt(i % bookerList.Count),
                    CourseInstance = new CourseInstance() { Course = new Course() { ID = "REQ: " + i.ToString() } },
                    LectureCount = Rand.Next(5, 35),
                    LectureLength = 2,
                    Priority = Rand.Next(2) == 1 ? Priority.Time : Priority.Room,
                    RequiredCapacity = Rand.Next(15, 30),
                    RoomPattern = MultiBooker.RoomList.ElementAt(Rand.Next(0, MultiBooker.RoomList.Count)).ID,
                    Time = RandRange(Rand, 8, 10, 13, 15),
                    Year = 2011,
                    StartPeriod = 1,
                    AllowedDays = new Dictionary<DayOfWeek, DayType>()
                    {
                        { DayOfWeek.Monday, DayType.Every },
                        { DayOfWeek.Tuesday, DayType.Every },
                        { DayOfWeek.Wednesday, DayType.Odd }
                    }

                });
                Console.WriteLine(Requests.Last());
            }
            UserList = bookerList;
        }
        private static int RandRange(Random Rand, params int[] range)
        {
            return range.ElementAt(Rand.Next(0, range.Length));
        }
    }
}
